package cn.com.b2office.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.com.b2office.bean.AccountStat;
import cn.com.b2office.dao.AccountActivityDao;
import cn.com.b2office.dao.CountyDao;
import cn.com.b2office.dao.MemberDao;
import cn.com.b2office.domain.City;
import cn.com.b2office.domain.County;
import cn.com.b2office.domain.District;
import cn.com.b2office.domain.Member;
import cn.com.b2office.domain.MemberAddress;
import cn.com.b2office.service.MemberService;

public class MemberServiceImpl implements MemberService {

	private MemberDao memberDao = null;

	private CountyDao countyDao = null;

	private AccountActivityDao accountActivityDao = null;

	public List<MemberAddress> findAllAddress(String memberId) {
		return memberDao.listAddresses(memberId);
	}

	public Map<String, String> translateAddress(List<MemberAddress> addressList) {
		Map<String, String> map = new HashMap<String, String>();
		for (MemberAddress address : addressList) {
			map.put(address.getAddidno(), translateAddress(address));
		}
		return map;
	}

	/**
	 * 获取地址的文字描述 [市]+[地区]+[社区]+[社区详细地址]
	 */
	public String translateAddress(MemberAddress address) {
		County county = countyDao.getCounty(address.getCountyid());
		if (county == null)
			throw new RuntimeException("county not exist! countyid="
					+ address.getCountyid());
		StringBuilder sb = new StringBuilder();
		City city = countyDao.getCity(county.getCity());
		if (city != null)
			sb.append(city.getCname()).append(" ");
		District dist = countyDao.getDistrict(county.getDistrict());
		sb.append(dist.getDname()).append(" ");
		sb.append(county.getCname()).append(" ");
		sb.append(address.getAddr()).append(" ");
		// sb.append(address.getCname());
		return sb.toString();
	}

	public MemberAddress getMemberAddress(String addressId) {
		return memberDao.getAddress(addressId);
	}

	public void setMemberDao(MemberDao memberDao) {
		this.memberDao = memberDao;
	}

	public void setCountyDao(CountyDao countyDao) {
		this.countyDao = countyDao;
	}

	public void setAccountActivityDao(AccountActivityDao accountActivityDao) {
		this.accountActivityDao = accountActivityDao;
	}

	public boolean ifRegEmailExist(String regemail) {

		return memberDao.ifRegEmailExist(regemail);
	}

	public boolean ifRegNameExist(String regName) {

		return memberDao.ifRegNameExist(regName);
	}

	public void createMember(Member member) {
		memberDao.createMember(member);

	}

	public Member getMemberByEmail(String regEmail) {

		return memberDao.getMemberByEmail(regEmail);
	}

	public Member getMemberByMemberno(String memberno) {
		return memberDao.getMemberByMemberno(memberno);
	}

	public Member getMemberByMembernoAndEmail(String memberno, String email) {

		return memberDao.getMemberByMembernoAndEmail(memberno, email);
	}

	public Member getMemberByNameAndEmail(String regName, String email) {

		return memberDao.getMemberByNameAndEmail(regName, email);
	}

	public void initializeMemberPwd(String memberId, String pwd) {
		Member member = (Member) memberDao.getMember(memberId);
		if (member == null)
			throw new RuntimeException("can't find the member where memberId="
					+ memberId);
		member.setRegpwd(pwd);
		memberDao.updateMember(member);
	}

	@Override
	public Member getMember(String memberId) {

		return memberDao.getMember(memberId);
	}

	@Override
	public void updateMember(Member member) {
		memberDao.updateMember(member);
	}

	@Override
	public MemberAddress getRegAddress(String memberId) {

		return memberDao.getRegAddress(memberId);
	}

	@Override
	public void createAddress(MemberAddress address) {
		County county = countyDao.getCounty(address.getCountyid());
		if (county == null)
			throw new RuntimeException("cannot find county where id="
					+ address.getCountyid());
		address.setCity(county.getCity());
		address.setProvince(county.getProvince());
		address.setDistrict(county.getDistrict());
		address.setModifytime(new Date());
		MemberAddress regAddr = memberDao.getRegAddress(address.getMemberid());
		if (regAddr == null)
			address.setFlag(1);
		memberDao.createAddress(address);
	}

	public AccountStat getAccountStat(String memberId) {
		return accountActivityDao.getAccountStat(memberId);
	}

	public void updateMemberAddress(MemberAddress address) {
		address.setAddrflag(0);
		memberDao.updateAddress(address);
	}

}
